// wood 木

package wd

import (
	"app/wd/gomysql"
	"app/wd/goredis"
	"fmt"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/google/uuid"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

const (
	EnvDev = "dev"
	EnvPro = "pro"
)

type _app struct {
	Config *viper.Viper
	Logger *logrus.Logger
	SysLog *logrus.Logger
	Mysql  map[string]*_Mysql
	Redis  map[string]*_Redis
	Env    string
}

var App = &_app{
	Mysql: make(map[string]*_Mysql),
	Redis: make(map[string]*_Redis),
}

// init 初始化框架
func init() {
	initConfig()
	initLogs()
	initMysql()
	initRedis()
}

func (a _app) Close() {
	gomysql.Close()
	goredis.Close()
	LogClose()
}

// Signal 信号处理
func (a _app) Signal() {
	s1 := make(chan os.Signal)
	s2 := make(chan struct{})
	go func() {
		signal.Notify(s1, syscall.SIGINT, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
		// 等待退出信号
		<-s1
		fmt.Println("收到信号 关闭信号")
		close(s2)
	}()

	<-s2
	fmt.Println("程序退出中")
	a.Close()
	//time.Sleep(5 * time.Second)
}

// GetTraceID 获取trace_id
func GetTraceID() string {
	return strings.ReplaceAll(uuid.New().String(), "-", "")
}
